// 对user操作的model
import React from 'react';
import {Model, Effect} from 'dva-core-ts';
import {Reducer} from 'redux';
import axios from 'axios';
import AsyncStorage from '@react-native-async-storage/async-storage';
import Toast from 'react-native-root-toast';
export interface ICarousel {
  carousel_id:string;
  carousel_url:string;
}
export interface IGoods {
  goods_id: string;
  goods_image: string;
  goods_title: string;
  money: number;
  goods_place: string;
  goods_brand: string;
  goods_type: number;
  second_types: number;
  buniness: {
    business_id: number;
    business_scurpture: string;
    business_name: string;
    business_introduction: string;
  };
}
export interface ILeavedUnused {
  carousels:ICarousel[],
  goods:IGoods[]
}
export interface GoodsModelState {
  leavedUnuseds?: ILeavedUnused[];
}

const GOODS_URL = '/goods/list'
interface GoodsModel extends Model {
  namespace: 'goods';
  state?: GoodsModelState;
  reducers?: {
    updateState: Reducer<ILeavedUnused>;
  };
  effects?: {
    getGoodsList:Effect
  };
}
const initValue = {
  leavedUnuseds:[{
    carousels:[],
    goods:[]
  }]
};
const goods: GoodsModel = {
  namespace: 'goods',
  state: initValue,
  reducers: {
    updateState: (state, {payload}) => {
      return {
        ...state,
        ...payload,
      };
    },
  },
  effects: {
    *getGoodsList({callback}, {call, put, select}): any {
      const res = yield call(axios.get, GOODS_URL);
      console.log("进来了res",res)
      if (res.data.code === 200) {
        console.log("商品数据",res.data.data)
        yield put({
          type: 'updateState',
          payload: {
            leavedUnuseds: res.data.data,
          },
        });
        // const { users } = yield select(state => state);
        if (callback && typeof callback === 'function') {
          callback(res.data.data);
        }
      }
    },
  },
};
export default goods;
